package com.dd.util;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.GZIPInputStream;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import com.dd.ma.MaUtil;

public class Test {
	// private String url="http://www.jkb114.com/tslf/";
	private int i;

	public Test() {
		// this.i=i;
		// this.start();
	}

	private String url = "http://222.77.187.25/DD/shop.do?method=findrestaurant&pageno=1&pagesize=10&query=";

	// private String
	// url="http://www.xiaomishu.com/shop/search/oD9/?type=res&key=%e6%b5%a6%e4%b8%9c";

	public void run() {
		String str = "";
		try {
			long l1 = System.currentTimeMillis();
			// String url1 = URLEncoder.encode("|","utf-8");
			// System.out.println(url1);
			// HttpsURLConnection hul;
			HttpClient httpClient = new HttpClient();
			GetMethod getMethod = new GetMethod(url);
			int statusCode = httpClient.executeMethod(getMethod);
			// System.out.println("postMethod()===" +
			// getMethod.getStatusLine());
			str = getMethod.getResponseBodyAsString();
			System.out
					.println(i + " use :" + (System.currentTimeMillis() - l1));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static double EARTH_RADIUS = 6378.137;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public static double GetDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		// s = Math.round(s * 10000) / 10000;
		return s;
	}
	
	public static void main(String arg[]) {
		try {
			//XftUvIZMJrhujzn8BFmIm3x5myUt6cuXIfXGbX652fGk5rA3AOZWlqyKbHM203fKUpJAQ1hU/fi22W9vPR7jl4fRGkHduq7h6PUy35UN0gW0Dfia0afEZ1YnNWKFf2mXI34othw9cG8rN/%2b1PPozfg
			//&C=chy+BEgRYZ1lTBASZ/sWjj7OpI2p/rQKvjqAxiEVz1/HOGvWF10MZHbPBHC7Jg1QZAYkDH7v0n9Y/boRFRm9k83hsHZFRs+yfEqapAl7vSI&lr=/fIaYsWMO6F8f4m5TnwTGw

			//System.out.println(URLDecoder.decode("&C=chy%2bBEgRYZ1lTBASZ/sWjj7OpI2p/rQKvjqAxiEVz1/HOGvWF10MZHbPBHC7Jg1QZAYkDH7v0n9Y/boRFRm9k83hsHZFRs%2byfEqapAl7vSI&lr=/fIaYsWMO6F8f4m5TnwTGw","utf-8"));
			System.out.println(getSndaRequestString1("XftUvIZMJrhujzn8BFmIm3x5myUt6cuXIfXGbX652fGk5rA3AOZWlqyKbHM203fKUpJAQ1hU/fi22W9vPR7jl4fRGkHduq7h6PUy35UN0gW0Dfia0afEZ1YnNWKFf2mXI34othw9cG8rN/+1PPozfg"));
			//System.out.println(getSndaRequestString1("/fIaYsWMO6F8f4m5TnwTGw"));
													//chy+BEgRYZ1lTBASZ/sWjj7OpI2p/rQKvjqAxiEVz1/HOGvWF10MZHbPBHC7Jg1QZAYkDH7v0n9Y/boRFRm9k83hsHZFRs+yfEqapAl7vSI&lr=/fIaYsWMO6F8f4m5TnwTGw
			//&C=chy+BEgRYZ1lTBASZ/sWjj7OpI2p/rQKvjqAxiEVz1/HOGvWF10MZHbPBHC7Jg1QZAYkDH7v0n9Y/boRFRm9k83hsHZFRs+yfEqapAl7vSI&lr=/fIaYsWMO6F8f4m5TnwTGw
			// 18650777744
			// http://game1-cbt.ma.sdo.com:10001/connect/app/login?cyt=1&login_id=18650777744&password=s7837750
			// http://game1-cbt.ma.sdo.com:10001/connect/app/login?cyt=1&login_id=13705050638&password=s7837750
			// http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairybattle?cyt=1&serial_id=16783017&user_id=552260
			// http://game1-cbt.ma.sdo.com:10001/connect/app/menu/fairyselect?cyt=1
			// s52l755p2i6tnbi4js44tkq2b1
			//&login_id=znIE3jBuHTRyadCl4lPG8A&password=N2QiXntzujsMFK3Xu2bbMQ
			//XftUvIZMJrhujzn8BFmIm3x5myUt6cuXIfXGbX652fGk5rA3AOZWlqyKbHM203fKUpJAQ1hU/fi22W9vPR7jl4fRGkHduq7h6PUy35UN0gW0Dfia0afEZ1YnNWKFf2mXI34othw9cG8rN/+1PPozfg
			HttpClient httpClient = new HttpClient();
			String url = "http://game1-cbt.ma.sdo.com:10001/connect/app/menu/fairyselect?cyt=1";
			//String url = "http://game1-cbt.ma.sdo.com:10001/connect/app/exploration/fairybattle?cyt=1";
			PostMethod postMethod = new PostMethod(url);
			postMethod.setRequestHeader("cookie","S=5mg2rajk1grmj8d0e4muk53uv2");
			postMethod.setRequestHeader("User-Agent", "Million/1.0.1 (iPhone; iPhone3,1; 6.0.1)");
			postMethod.setRequestHeader("Accept-Encoding", "gzip");
			NameValuePair[] data = {
					new NameValuePair("check", MaUtil
							.getSndaRequestString("1")),
					new NameValuePair("serial_id", MaUtil
							.getSndaRequestString("17713671")),
					new NameValuePair("user_id", MaUtil
							.getSndaRequestString("53024")) };
			// 将表单的值放入postMethod中
		//	postMethod.setRequestBody(data);
			// 执行postMethod

			int statusCode = httpClient.executeMethod(postMethod);
			GZIPInputStream gis = null;
			gis = new GZIPInputStream(postMethod.getResponseBodyAsStream());
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] b = new byte[0x400];
			int n;
			while ((n = gis.read(b)) != -1) baos.write(b, 0, n);
			System.out.println("Gzipped");
			byte[] bb=baos.toByteArray();

			
			SecretKeySpec keyspec = new SecretKeySpec("rBwj1MIAivVN222b".getBytes(),"AES");
			Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
			c.init(Cipher.DECRYPT_MODE, keyspec);
			byte[] bbb=c.doFinal(bb);
			String str = new String(bbb);
			System.out.println(str);

		} catch (Exception e) {
			e.printStackTrace();
		}

		
	}
	public static String getSndaRequestString(String input) {
		try {
			SecretKeySpec keyspec = new SecretKeySpec("rBwj1MIAivVN222b"
					.getBytes(), "AES");
			Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
			c.init(Cipher.ENCRYPT_MODE, keyspec);
			String output = Base64.encodeBase64String(c.doFinal(input
					.getBytes()));
			if (output.endsWith("=")) {
				output = output.substring(0, output.length() - 1);
			}
			if (output.endsWith("=")) {
				output = output.substring(0, output.length() - 1);
			}
			return output;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return input;
	}
	public static String getSndaRequestString1(String input) {
		try {
			SecretKeySpec keyspec = new SecretKeySpec("rBwj1MIAivVN222b"
					.getBytes(), "AES");
			Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
			c.init(Cipher.DECRYPT_MODE, keyspec);

			String output = new String(c.doFinal(Base64.decodeBase64(input)));
			return output;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return input;
	}
}